package org.corolla.cloth.web.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.SessionActionMapping;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.corolla.cloth.compute.ComputTranspotFees;
import org.corolla.cloth.constdata.ClothSizeConst;
import org.corolla.cloth.entity.Order;
import org.corolla.cloth.service.OrderService;

import sun.tools.tree.ThisExpression;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "struts-default")
@Results({
		@Result(name = "input", type = "redirect", location = "/view/PriceSheet.jsp"),
		@Result(name = "success", type = "redirect", location = "/view/Car.jsp") })
@Namespace("/view")
@Action(value = "carAction")
public class CarAction extends ActionSupport {

	/**
     * 
     */
	private static final long serialVersionUID = 1L;
	@Resource(name = "orderService")
	private OrderService orderService;

	@Override
	public String execute() throws Exception {
		orderService = new OrderService();
		// 程序运行到execute()时，前面的验证都已经正确了
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();

		List<Order> orderlist = null;
		List<Order> orderPricelist = null;
        boolean access = false;
        Date date = new Date();
        if (date.getHours()>21) {
            access = true;
        }

		try {
			List<Order> orderalllist = orderService.findAllOrders();
			orderPricelist = (List<Order>) session
					.getAttribute("orderPricelist");
			Map<String, Order> ordermap = new HashMap<String, Order>();
			if (null != orderalllist) {
				if (orderalllist.size() > 0) {
					for (int i = 0; i < orderalllist.size(); i++) {
						ordermap.put(orderalllist.get(i).getClothSize().trim(),orderalllist.get(i));
					}
				}
			}

			if (null != orderPricelist) {
				for (int i = 0; i < orderPricelist.size(); i++) {
					if (null != orderPricelist.get(i)) {
						if (null != ordermap.get(orderPricelist.get(i)
								.getClothSize())) {
							Order ord = ordermap.get(orderPricelist.get(i)
									.getClothSize());
							ord.setClothMount(ord.getClothMount()
									+ orderPricelist.get(i).getClothMount());
							ord.setTotalPriceBySize(ord.getTotalPriceBySize()
									+ orderPricelist.get(i)
											.getTotalPriceBySize());
							orderService.upadteCountByPk(ord);
						} else {
							orderService.saveOrder(orderPricelist.get(i));
						}
					}
				}
			}
			orderlist = orderService.findAllOrders();
			double producttotal = 0.0;
			double alltotal = 0.0;
			int count = 0;
			if (null != orderlist) {
				if (orderlist.size() > 0) {
					for (int i = 0; i < orderlist.size(); i++) {
						producttotal = producttotal
								+ orderlist.get(i).getTotalPriceBySize();
						count = count + orderlist.get(i).getClothMount();
					}
				}
			}
			double transportfees = 0.0;
			if (null != session.getAttribute("transportfees")) {
				transportfees = Double.valueOf(session.getAttribute(
						"transportfees").toString());
			} else {
				transportfees = ComputTranspotFees.getTransportFees(count);
			}
			alltotal = producttotal + transportfees;
			session.setAttribute("orderlist", orderlist);
			session.setAttribute("sizenum", null);
			session.setAttribute("checksize", null);
			session.setAttribute("checkindex", 0);
			session.setAttribute("liningredioid", 0);
			session.setAttribute("redioid", 0);
			session.setAttribute("transportfees", transportfees);
			session.setAttribute("totelprice", producttotal);
			session.setAttribute("totalcountprice", alltotal);
//            if(access)
                return SUCCESS;
		} catch (Exception ex) {
			ex.printStackTrace();
			return "input";
		}
//		return "input";
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

}
